<script>
import {
  Scene,
  PerspectiveCamera,
  WebGLRenderer,
  Vector3,
  LineBasicMaterial,
  Geometry,
  Line
} from 'three'

import Detector from 'three/examples/js/Detector'
export default {
  template: '<div style="width: 100%; height: 100%"></div>',
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      cube: null,
      ele: null
    }
  },
  mounted() {
    this.ele = this.$el

    if (Detector.webgl) {
      this.init()
    } else {
      const warning = Detector.getWebGLErrorMessage()
      this.ele.appendChild(warning)
    }
  },
  methods: {
    init() {
      const width = this.ele.clientWidth
      const height = this.ele.clientHeight
      this.renderer = new WebGLRenderer()
      this.renderer.setSize(width, height)
      this.ele.appendChild(this.renderer.domElement)
      this.renderer.setClearColor(0xffffff, 1)

      this.camera = new PerspectiveCamera(45, width / height, 1, 500)
      this.camera.position.set(0, 0, 100)
      this.camera.lookAt(new Vector3(0, 0, 0))

      this.scene = new Scene()

      const material = new LineBasicMaterial({ color: 0x0000ff })
      const geometry = new Geometry()
      geometry.vertices.push(new Vector3(-10, 0, 0))
      geometry.vertices.push(new Vector3(0, 10, 0))
      geometry.vertices.push(new Vector3(10, 0, 0))

      const line = new Line(geometry, material)
      this.scene.add(line)
      this.renderer.render(this.scene, this.camera)
    }
  }
}
</script>